#!/usr/bin/php
<?php
$_SERVER['DOCUMENT_ROOT'] = exec('pwd').'/';

require_once(
  $_SERVER['DOCUMENT_ROOT'].'../application/controllers/autoload.class.php'
);
autoload::init();

$dbc = db_controller::get_instance();

if ($_SERVER['argc'] == 2) {
    $single_user_mode = true;
    $user_id = $dbc->escape(intval($_SERVER['argv'][1]));
    $q = 'SELECT id FROM user WHERE id='.$user_id;
}
else {
    $single_user_mode = false;
    $q = 'SELECT id FROM user';
}

$users_raw = $dbc->query_assoc($q);

foreach ($users_raw as $user_raw) {
    try {
        $user = new user($user_raw['id']);

        $twitter_api_connection = new twitter_api_connection();
        $twitter_api_connection->set_user_credentials(
          $user->get_credentials()
        );
        $twitter_api_connection->connect_and_verify();

        $user->set('name', $twitter_api_connection->get_user_name());
        $user->set('screen_name', $twitter_api_connection->get_user_screen_name());
        $user->save();

        $ff_ids_local = $user->get_ff_ids();
        $ff_ids_twitter = $twitter_api_connection->get_ff_ids();

        $ff_ids_remove = array_diff($ff_ids_local, $ff_ids_twitter);
        $ff_ids_add = array_diff($ff_ids_twitter, $ff_ids_local);
        $ff_ids_update = array_intersect($ff_ids_twitter, $ff_ids_local);

        if (count($ff_ids_remove)) {
            if ($single_user_mode) {
                $q = 'DELETE FROM user_follower '
                    .'WHERE (user_id='.$user->get_id().' '
                    .'AND follower_id IN ('.implode(', ', $ff_ids_remove).')) '
                    .'OR (follower_id='.$user->get_id().' '
                    .'AND user_id IN ('.implode(', ', $ff_ids_remove).'))';
            }
            else {
                $q = 'DELETE FROM user_follower '
                    .'WHERE user_id='.$user->get_id().' '
                    .'AND follower_id IN ('.implode(', ', $ff_ids_remove).')';
            }
            $dbc->query_assoc($q);
        }

        if (count($ff_ids_update)) {
            $q = 'UPDATE user_follower '
                .'SET last_update=NOW() '
                .'WHERE user_id='.$user->get_id().' '
                .'AND follower_id IN ('.implode(', ', $ff_ids_update).')';
            $dbc->query_assoc($q);
        }

        if (count($ff_ids_add)) {
            foreach ($ff_ids_add as $ff_id) {
                if ($single_user_mode) {
                    $q = 'INSERT IGNORE INTO user_follower (user_id, follower_id) VALUES '
                        .'('.$user->get_id().','.$ff_id.'), '
                        .'('.$ff_id.','.$user->get_id().') '
                        .'ON DUPLICATE KEY UPDATE last_update=NOW()';
                }
                else {
                    $q = 'INSERT IGNORE INTO user_follower (user_id, follower_id) VALUES '
                        .'('.$user->get_id().','.$ff_id.') '
                        .'ON DUPLICATE KEY UPDATE last_update=NOW()';
                }
                $dbc->query_assoc($q);
            }
        }
    }
    catch (tds_exception $e) {

    }
}
?>